package net.pzc.filechooser;

import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class FileItemTableCellRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = 1L;

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		if (isSelected) {
			setForeground(table.getSelectionForeground());
			setBackground(table.getSelectionBackground());
		} else {
			setForeground(table.getForeground());
			setBackground(table.getBackground());
		}
		if (value != null) {
			if (value instanceof BufferedImage) {
				// "Icon" column
				setText("");
				setIcon(new ImageIcon((BufferedImage) value));
				if (isSelected) {
					setBackground(new Color(212, 208, 200));
				}
			} else {
				if (column == 2) {
					// "Size" column
					setHorizontalAlignment(JLabel.RIGHT);
					setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
				} else {
					setHorizontalAlignment(JLabel.LEFT);
					setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
				}
				setText(value.toString());
				setIcon(null);
				if (isSelected) {
					setBackground(new Color(10, 36, 106));
				}
			}
		}

		return this;
	}
}
